package com.novacloud.fresh.service.impl.login;

import com.novacloud.fresh.dao.login.UsersMapper;
import com.novacloud.fresh.model.login.Users;
import com.novacloud.fresh.model.login.UsersExample;
import com.novacloud.fresh.service.login.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version 1.0
 * @date 2016/4/18 14:29
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;

    /**
     *
     * @param userName
     * @return
     */
    @Override
    public Users getByUserName(String userName) {
        UsersExample usersExample = new UsersExample();
        usersExample.createCriteria().andUsernameEqualTo(userName);
        List<Users> users = usersMapper.selectByExample(usersExample);
        if (users.size() != 1) {
            throw new IncorrectResultSizeDataAccessException(users.size());
        } else {
            return users.get(0);
        }
    }
}
